package dao;

// Generated Nov 16, 2011 12:44:55 PM by Hibernate Tools 3.4.0.CR1

import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;

import pojo.Tvshow;

/**
 * Home object for domain model class Tvshow.
 * @see pojo.Tvshow
 * @author Hibernate Tools
 */
public class TvshowHome {

    private static final Log log = LogFactory.getLog(TvshowHome.class);

    private final SessionFactory sessionFactory = getSessionFactory();

    protected SessionFactory getSessionFactory() {
	try {
	    return (SessionFactory) HibernateUtil.getSessionFactory();
	} catch (Exception e) {
	    log.error("Could not locate SessionFactory in JNDI", e);
	    throw new IllegalStateException(
		    "Could not locate SessionFactory in JNDI");
	}
    }

    public void persist(Tvshow transientInstance) {
	log.debug("persisting Tvshow instance");
	try {
	    Session session = sessionFactory.openSession();
	    session.persist(transientInstance);
	    log.debug("persist successful");
	    session.close();
	} catch (RuntimeException re) {
	    log.error("persist failed", re);
	    throw re;
	}
    }

    public void attachDirty(Tvshow instance) {
	log.debug("attaching dirty Tvshow instance");
	try {
	    Session session = sessionFactory.openSession();
	    session.saveOrUpdate(instance);
	    log.debug("attach successful");
	    session.flush();
	    session.close();
	} catch (RuntimeException re) {
	    log.error("attach failed", re);
	    throw re;
	}
    }

    public void attachClean(Tvshow instance) {
	log.debug("attaching clean Tvshow instance");
	try {
	    Session session = sessionFactory.openSession();
	    session.lock(instance, LockMode.NONE);
	    log.debug("attach successful");
	    session.close();
	} catch (RuntimeException re) {
	    log.error("attach failed", re);
	    throw re;
	}
    }

    public void delete(Tvshow persistentInstance) {
	log.debug("deleting Tvshow instance");
	try {
	    Session session = sessionFactory.openSession();
	    session.delete(persistentInstance);
	    log.debug("delete successful");
	    session.flush();
	    session.close();
	} catch (RuntimeException re) {
	    log.error("delete failed", re);
	    throw re;
	}
    }

    public Tvshow merge(Tvshow detachedInstance) {
	log.debug("merging Tvshow instance");
	try {
	    Session session = sessionFactory.openSession();
	    Tvshow result = (Tvshow) session.merge(
		    detachedInstance);
	    log.debug("merge successful");
	    session.close();
	    return result;
	} catch (RuntimeException re) {
	    log.error("merge failed", re);
	    throw re;
	}
    }

    public Tvshow findById(java.lang.Integer id) {
	log.debug("getting Tvshow instance with id: " + id);
	try {
	    Session session = sessionFactory.openSession();
	    Tvshow instance = (Tvshow) session.get(
		    "pojo.Tvshow", id);
	    if (instance == null) {
		log.debug("get successful, no instance found");
	    } else {
		log.debug("get successful, instance found");
	    }
	    session.close();
	    return instance;
	} catch (RuntimeException re) {
	    log.error("get failed", re);
	    throw re;
	}
    }
    
    public List findByKeyword(String keyword) {
	log.debug("finding Movie instance by example");
	try {
	    Session session = sessionFactory.openSession();
	    Query query =  session.createQuery("from pojo.Tvshow tv where tv.tvTitle like '%"+keyword+"%'");
	    
	    List results = query.list();
	  
	    session.close();
	    return results;
	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	}
    }
    
    public List findByChannel(int channelid) {
	log.debug("finding Movie instance by example");
	try {
	    Session session = sessionFactory.openSession();
	    Query query = session.createSQLQuery("select * from tvshow where tv_channel="+ channelid).addEntity(Tvshow.class);
	    List results = query.list();
	    session.close();
	    return results;
	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	}
	
	
    }

    public List findByExample(Tvshow instance) {
	log.debug("finding Tvshow instance by example");
	try {
	    Session session = sessionFactory.openSession();
	    List results = session
		    .createCriteria("pojo.Tvshow")
		    .add(Example.create(instance)).list();
	    log.debug("find by example successful, result size: "
		    + results.size());
	    session.close();
	    return results;
	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	}
    }
}
